//
//  ShopMenuController.m
//  takeout
//
//  Created by sengci on 12-2-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ShopMenuController.h"
#import "DishManager.h"
#import "Dish.h"
#import "Constants.h"

@implementation ShopMenuController
@synthesize styleOfCookingList;

- (void)viewDidLoad
{
    //设置视图title
    self.title= [recieveDictionary objectForKey:@"supName"];
    //设置返回上一页按钮
    //第一个参数为按钮标题
    //第二个参数是按钮样式
    //第三个参数接受消息的对象
    //第四个参数是按钮的回调函数
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,54,27)];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"Detail-002.png"] forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"Detail-002.png"] forState:UIControlStateHighlighted];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    leftButton.titleLabel.font=[UIFont systemFontOfSize:14]; 
    [leftButton addTarget:self action:@selector(backButtonPressd) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    //设置为左边按钮
    [[self navigationItem] setLeftBarButtonItem:leftItem];
    [leftButton release];
    [leftItem release];
    
    //修改导航栏背景
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"001s.png"] forBarMetrics: UIBarMetricsDefault];
    
    leftTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 90, 480)];
    leftTable.delegate = self;
    leftTable.dataSource = self;
    leftTable.tag = 1;
    leftTable.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight; 
    UIView *leftFooter = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 90, 60)];
    leftTable.tableFooterView = leftFooter;
    [leftFooter release];
    
    rightTable = [[UITableView alloc] initWithFrame:CGRectMake(90, 40, 230, 480)]; 
    rightTable.delegate = self;
    rightTable.dataSource = self;
    rightTable.tag = 2;
    rightTable.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight;  
    UIView *rightFooter = [[UIView alloc] initWithFrame:CGRectMake(90, 480, 230, 60)];
    rightTable.tableFooterView = rightFooter;
    [rightFooter release];
    
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"leftPanelBg.png"]];
    [leftTable setBackgroundView:bgView];
    [bgView release];
    
    [self.view addSubview:leftTable];
    [self.view addSubview:rightTable];
    
    //从后台获取所需的数据
    DishManager *manager = [[DishManager alloc] init];
    fmdbDishManager=[[FMDBDishManager alloc]init];
    appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;	
    if([appDelegate status])
    {
        //联网，添加到本地
        dishCategories = [manager getAllDishesBySupId:(NSString *)[recieveDictionary objectForKey:@"supId"]];
        //插入本地
        NSInteger supid=[[recieveDictionary objectForKey:@"supId"]intValue];
        [fmdbDishManager insertAllDishesBySupIdToLoc:dishCategories supplierid:supid];
    }
    else{
        dishCategories=[fmdbDishManager getAllDishesBySupIdFromLoc:[recieveDictionary objectForKey:@"supId"]];
    }
    
    //新建一个“全部”类别的对象，并将各类的数据全部放入此对象中
    DishCategory *allDishCategory = [[DishCategory alloc] init];
    allDishCategory.dishCategoryId = 0;
    allDishCategory.dishCategoryName = @"全部";
    for (int i = 0; i < [dishCategories count]; i++) {
        NSArray *dishes = ((DishCategory *)[dishCategories objectAtIndex: i]).dishes;
        if (allDishCategory.dishes == nil) {
            NSArray *tmpArray = [[NSArray alloc] initWithArray:dishes];
            allDishCategory.dishes = tmpArray;
            [tmpArray release];
        }
        else{
            allDishCategory.dishes = [allDishCategory.dishes arrayByAddingObjectsFromArray:dishes];  
        }
    }
    //将“全部”类别的对象和从后台获取的各类别的数据放入数组中
    dishCategoriesForUse = [[NSArray alloc] initWithObjects:allDishCategory, nil];
    dishCategoriesForUse = [dishCategoriesForUse arrayByAddingObjectsFromArray:dishCategories];
    //设置页面所要显示的数据信息
    currentDishCategory = (DishCategory *)[dishCategoriesForUse objectAtIndex:0];
    self.styleOfCookingList = dishCategoriesForUse;
    [manager release];
    [allDishCategory release];
    
    UIImageView *topView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"topBg.png"]];
    [topView setFrame:CGRectMake(0, -4, 320, 40)];
    [self.view addSubview:topView];
    [topView release];
    
    [super viewDidLoad];
}

- (void)backButtonPressd{
    //将当前视图推出栈，程序将进入上一视图
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rootButtonPressd{
    //直接进入根视图
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//返回表试图分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView.tag==1){
        return [dishCategoriesForUse count];
    }else{
        return [currentDishCategory.dishes count];
    }
    [self viewDidLoad];
}

//设置表试图每行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView.tag == 1)
    {
        static NSString *identifier = @"TypeIdentifier";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        if(cell==nil){
            cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
        }
        NSUInteger row = [indexPath row];
        DishCategory *dishCategory = (DishCategory *)[styleOfCookingList objectAtIndex:row];

        cell.textLabel.text = [NSString stringWithFormat:@"%@",dishCategory.dishCategoryName];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
        cell.textLabel.textColor = [UIColor brownColor];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        
        [cell setTag:dishCategory.dishCategoryId];
        
        UIImageView *defaultBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"defaultBg.png"]];
        UIImageView *touchBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"touchBg.png"]];
        UIImageView *selectBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selectBg.png"]];
        
        [cell setBackgroundView:defaultBgView];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectedBackgroundView = touchBgView;
        cell.editingAccessoryView = selectBgView;
        
        [defaultBgView release];
        [touchBgView release];
        [selectBgView release];
        
        return cell;
    }
    else
    {
        static NSString *CellIdentifier = @"DishCellIdentifier";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }else {
            RemoveAllSubViews(cell.contentView);
        }
        
        NSInteger row = [indexPath row];
        Dish *dish =  [currentDishCategory.dishes objectAtIndex:row];
        UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:100];
        if (nameLabel == nil) {
            nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 15, 200, 16)];
            [nameLabel setText:dish.dishName];
            [nameLabel setFont:[UIFont boldSystemFontOfSize:16]];
            [nameLabel setBackgroundColor:[UIColor clearColor]];
            nameLabel.tag = 100;
            [cell.contentView addSubview:nameLabel];
            [nameLabel release];
        }

        UILabel *priceLabel = (UILabel *)[cell.contentView viewWithTag:200];
        if (priceLabel == nil) {
            priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 45, 80, 16)];
            [priceLabel setText:[NSString stringWithFormat:@"¥ %0.2f",dish.price]];
            [priceLabel setTextColor:[UIColor colorWithRed:255.0/255.0 green:111.0/255.0 blue:6.0/255.0 alpha:1.0f]];
            [priceLabel setFont:[UIFont boldSystemFontOfSize:16]];
            [priceLabel setBackgroundColor:[UIColor clearColor]];
            priceLabel.tag = 200;
            [cell.contentView addSubview:priceLabel];
            [priceLabel release];
        }
        
        if (dish.secondPrice != 0 && dish.secondPrice != PRICE_BY_TIME) {
            UILabel *secondPriceLabel = (UILabel *)[cell.contentView viewWithTag:201];
            if (secondPriceLabel == nil) {
                secondPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 45, 80, 16)];
                [secondPriceLabel setText:[NSString stringWithFormat:@"¥ %0.2f",dish.price]];
                [secondPriceLabel setTextColor:[UIColor colorWithRed:255.0/255.0 green:111.0/255.0 blue:6.0/255.0 alpha:1.0f]];
                [secondPriceLabel setFont:[UIFont boldSystemFontOfSize:16]];
                [secondPriceLabel setBackgroundColor:[UIColor clearColor]];
                secondPriceLabel.tag = 201;
                [cell.contentView addSubview:secondPriceLabel];
                [secondPriceLabel release];
            }
        }
        
        if (dish.thirdPrice != 0 && dish.thirdPrice != PRICE_BY_TIME) {
            UILabel *thirdPriceLabel = (UILabel *)[cell.contentView viewWithTag:202];
            if (thirdPriceLabel == nil) {
                thirdPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(165, 45, 80, 16)];
                [thirdPriceLabel setText:[NSString stringWithFormat:@"¥ %0.2f",dish.price]];
                [thirdPriceLabel setTextColor:[UIColor colorWithRed:255.0/255.0 green:111.0/255.0 blue:6.0/255.0 alpha:1.0f]];
                [thirdPriceLabel setFont:[UIFont boldSystemFontOfSize:16]];
                [thirdPriceLabel setBackgroundColor:[UIColor clearColor]];
                thirdPriceLabel.tag = 202;
                [cell.contentView addSubview:thirdPriceLabel];
                [thirdPriceLabel release];
            }
        }
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.tag = dish.dishId;
        return cell;
    }
}

//设置表试图行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath   
{
    if(tableView.tag==1){
        return 50;
    }else{
        return 70;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView.tag == 1){
        NSInteger row = [indexPath row];
        currentDishCategory = (DishCategory *)[dishCategoriesForUse objectAtIndex:row];
        [rightTable reloadData];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload
{
    [self setStyleOfCookingList:nil];
    [super viewDidUnload];
}

- (void)dealloc {
    [styleOfCookingList release];
    [leftTable release];
    [rightTable release];
    [currentDishCategory release];
    [dishCategoriesForUse release];
    [super dealloc];
}
@end
